import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';
import { CustomizedCouponService } from '../services/customized-coupon.service';

@Component({
  selector: 'app-customized-coupon-details',
  templateUrl: './customized-coupon-details.component.html',
  styleUrls: ['./customized-coupon-details.component.scss']
})
export class CustomizedCouponDetailsComponent implements OnInit {

  routeParams: any = {};
  basicInfos: any = {}; // 基础信息
  useInfos: any = {}; // 适用信息
  categoryOptions: any = []; // 适用用户

  constructor(
    private message: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private services: CustomizedCouponService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  ngOnInit(): void {
    this.loadDetailsData()
  }

  // 查询适用品类options
  async loadAdzGoodTypeList() {
    const {code, data, message}: any = await this.services.getAdzGoodTypeList();
    if (code === 0) {
      this.categoryOptions = data || [];
    } else {
      this.message.error(message);
    }
  }

  async loadDetailsData() {
    this.loadAdzGoodTypeList();

    let params = {
      id: this.routeParams.id
    }
    const messId = this.message.loading('数据加载中..', { nzDuration: 0 }).messageId;
    const {code, data, message}: any = await this.services.getCouponDetails(params);
    this.message.remove(messId);
    
    if (code === 0) {
      const {rechargeCouponInfo, useCouponInfo}: any = data || {};
      this.basicInfos = rechargeCouponInfo || {}; // 基础信息
      this.useInfos = useCouponInfo || {}; // 适用信息
    } else {
      this.message.error(message);
    }
  }

  // 适用品类Filter
  categoryTypeFilter(str: string) {
    if (this.isEmpty(str)) {
      return '-'
    } else {
      if (str === '0') {
        return '全部'
      } else {
        let strArr = str.split(',').map((m: any) => +m);
        let tempArr: any = [];
        this.categoryOptions.forEach((item: any) => {
          if (strArr.includes(item.groupId)) {
            tempArr.push(item.textureName);
          }
        })
        return tempArr.join('、');
      }
    }
  }

  // is Empty
  isEmpty(val: any) {
    if (val === '' || val === undefined || val === null) {
      return true;
    } else {
      return false;
    }
  }
}
